<template>
  <Dialogdrawerdefect :ddtvisible="dialogVisible" size="360px" :has-modal="false" :modal="false" @close="handleClose" title="黑名单规则配置">
    <div class="col-right" v-loading="loading">
      <el-form inline class="rule-form" ref="form" :model="ruleForm" :rules="rules">
        <!-- <div class="sub-title">黑名单配置医院</div>
        <el-form-item label="医院代码：" prop="hospitalCode" class="short-input">
          <el-select size="small" class="param-type_input" v-model="ruleForm.hospitalCode" placeholder="请选择" @change="hospitalChange">
            <el-option
              v-for="(item,index) in mx_hospitalList"
              :key="index"
              :label="item.name"
              :value="item.code"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <div class="sub-title">黑名单触发规则</div>
        <el-form-item label="爽约有效期" prop="breakAppointmentDay" class="short-input">
          <el-select size="small" v-model="ruleForm.breakAppointmentDay" placeholder="请选择天数" clearable>
            <el-option
              v-for="(item,index) in timesOptions"
              :key="index"
              :label="item.item"
              :value="Number(item.itemValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="爽约：" prop="breakAppointmentNum" class="short-input">
          <el-input size="small" v-model.number="ruleForm.breakAppointmentNum"></el-input>
          <span class="word">次</span>
        </el-form-item>
        <div class="sub-title ky-mt10">解禁规则</div>
        <el-form-item label="自动解禁：" prop="autoBanDay" class="short-input">
          <el-select size="small" v-model="ruleForm.autoBanDay" placeholder="请选择天数" clearable>
            <el-option
              v-for="(item,index) in timesOptions"
              :key="index"
              :label="item.item"
              :value="Number(item.itemValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <div class="sub-title ky-mt10">申请限制</div>
        <el-form-item label="黑名单申请限制：" prop="appointmentType" class="short-input">
          <!-- <el-checkbox-group v-model="ruleForm.appointmentType">
            <el-checkbox label="转诊预约" name="type">转诊预约</el-checkbox>
            <el-checkbox label="外院预约" name="type">外院预约</el-checkbox>
            <el-checkbox label="临床预约" name="type">临床预约</el-checkbox>
            <el-checkbox label="自助预约" name="type">自助预约</el-checkbox>
          </el-checkbox-group> -->
          <el-select size="small" v-model="ruleForm.appointmentType" placeholder="请选择预约来源" clearable>
            <el-option
              v-for="(item,index) in typeOptions"
              :key="index"
              :label="item.item"
              :value="item.item"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="buttons">
        <div class="right-buttons">
          <el-button size="small" @click="handleClose">关 闭</el-button>
          <el-button size="small" type="primary" @click="handleConfirm">确 定</el-button>
        </div>
      </div>
    </div>
  </Dialogdrawerdefect>
</template>
<script>
import Dialogdrawerdefect from '@/components/dialogbox/dialogDrawerDefect'
import { getItemList, addBlackRuleList, getBlackRuleDetail, updateBlackRule } from '@/api/api.js'
import { PageStatus } from '@/utils/constant'

export default {
  name: 'EditDialog',
  components: {
    Dialogdrawerdefect
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    hospitalCode: {
      String,
      default: ''
    },
    id: {
      String,
      default: ''
    },
    currentStatus: {
      Symbol,
      default: null
    }
  },
  data() {
    return {
      pageStatus: PageStatus,
      ruleForm: {
        hospitalCode: '',
        breakAppointmentDay: null,
        breakAppointmentNum: null,
        autoBanDay: null,
        appointmentType: ''
      },
      loading: false,
      typeOptions: ['转诊预约', '外院预约', '临床预约', '自助预约'],
      rules: {
        hospitalCode: { required: true, message: '请选择医院', trigger: 'blur' },
        breakAppointmentDay: { required: true, message: '请选择爽约有效期', trigger: ['blur', 'change'] },
        breakAppointmentNum: { required: true, message: '请选择最大爽约次数', trigger: ['blur', 'change'] },
        autoBanDay: { required: true, message: '请选择自动解禁时间', trigger: ['blur', 'change'] },
        appointmentType: { required: true, message: '请选择黑名单申请限制', trigger: ['blur', 'change'] }
      },
      timesOptions: []
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.ruleForm.hospitalCode = this.hospitalCode
        this.getTimesOptions()
        this.getTypeOptions()
        if (this.currentStatus === this.pageStatus.edit) {
          this.getDetail()
        }
      }
    }
  },
  methods: {
    handleClose() {
      this.$refs.form.resetFields()
      this.$emit('update:dialogVisible', false)
    },
    getDetail() {
      this.loading = true
      getBlackRuleDetail({ id: this.id, hospitalCode: this.hospitalCode }).then(res => {
        this.loading = false
        if (res && res.code === 10000) {
          this.ruleForm = res.content
        }
      }).catch(() => {
        this.loading = false
      })
    },
    handleConfirm() {
      // 保存
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.currentStatus === this.pageStatus.add) { // 新增
            addBlackRuleList(this.ruleForm).then(res => {
              if (res && res.code === 10000) {
                this.$message.success('新增成功')
                this.$emit('refresh')
                this.handleClose()
              }
            })
          } else { // 修改
            updateBlackRule(this.ruleForm).then(res => {
              if (res && res.code === 10000) {
                this.$message.success('修改成功')
                this.$emit('refresh')
                this.handleClose()
              }
            })
          }
        } else {
          setTimeout(() => {
            var isError = document.getElementsByClassName('is-error')
            isError[0].querySelector('input').focus()
          }, 100)
          return false
        }
      })
    },
    hospitalChange() {
      this.getDetail()
    },
    getTimesOptions() {
      // 获取预约来源
      let data = {
        HospitalCode: this.ruleForm.hospitalCode,
        type: '解禁时间'
      }
      getItemList(data).then(res => {
        if (res && res.code === 10000) {
          this.timesOptions = res.content.list.sort((a, b) => {
            return a.itemValue - b.itemValue
          })
        }
      })
    },
    getTypeOptions() {
      let data = {
        HospitalCode: this.ruleForm.hospitalCode,
        type: '预约来源'
      }
      getItemList(data).then(res => {
        if (res && res.code === 10000) {
          this.typeOptions = res.content.list
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.col-right{
        border-left: 1px solid #e5e5e5;
        padding: 0px 20px 47px;
        color: #606266;
        background-color: #FFFFFF;
        .sub-title{
            font-weight: 600;
        }
        .short-input{
            width: 260px;
            margin-right: 23px!important;
            margin-bottom: 5px;
            .el-input,/deep/ .el-input__inner{
                width: 200px;
            }
            .word{
                margin-left: 5px;
            }
        }
        .el-form-item{
            /deep/ .el-form-item__label{
                font-weight: 500;
            }
        }
        .mini-input{
            width: 185px;
            margin-right: 15px !important;
            margin-bottom: 5px;
            .el-input{
                width: 185px;
            }
        }
        .large-width{
            width: 424px;
            /deep/ .el-textarea__inner{
                width: 424px;
            }
        }
        .describe-input{
            width: 350px;
            .el-input{
                width: 350px;
            }
        }
        .name{
            font-weight: bold;
        }
        .el-form--inline .el-form-item{
            margin-right:0px
        }
        .buttons{
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            background: #fff;
            padding: 5px 25px;
            border-top: 1px solid #E5E7EC;
            line-height: 31px;
            /deep/ .el-form-item__content{
                width:100%
            }
            .right-buttons{
                float:right
            }
            .el-dropdown{
                color: #00c0ef;
            }
        }
        .text-block{
            background: #f5f5f5;
            padding: 0 10px;
            margin-top: 10px;
        }
        .content-block{
           padding: 0 10px;
        }
    }
</style>
